package com.imooc.o2o.web.Exceladmin;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.imooc.o2o.dto.GradeSubjectExecution;
import com.imooc.o2o.dto.ReachDescExecution;
import com.imooc.o2o.dto.ReachPointExecution;
import com.imooc.o2o.dto.Result;
import com.imooc.o2o.entity.Excel;
import com.imooc.o2o.entity.GradeSubject;
import com.imooc.o2o.entity.ReachDesc;
import com.imooc.o2o.entity.ReachPoint;
import com.imooc.o2o.enums.GradeSubjectEnum;
import com.imooc.o2o.enums.ReachDescEnum;
import com.imooc.o2o.enums.ReachPointStateEnum;
import com.imooc.o2o.excptions.GradeSubjectOperationException;
import com.imooc.o2o.excptions.ReachDescOperationException;
import com.imooc.o2o.excptions.ReachPointOperationException;
import com.imooc.o2o.service.ReachDescService;
import com.imooc.o2o.service.ReachPointService;
import com.imooc.o2o.util.HttpServletRequestUtil;

@Controller
@RequestMapping(value = "/exceladmin")
public class ReachDescController {
	@Autowired
	private ReachDescService reachDescService;
	@RequestMapping(value = "/getreachdesc",method=RequestMethod.GET)
	@ResponseBody
	private Map<String, Object> getReachDescById(HttpServletRequest request){
		Map<String,Object> modelMap=new HashMap<String,Object>();
		Excel currentExcel=(Excel)request.getSession().getAttribute("currentExcel");
		ReachDesc list=null;
		if(currentExcel!=null&&currentExcel.getExcelId()>0) {
			try {
				list=reachDescService.getReachDescList(currentExcel.getExcelId());
				modelMap.put("reachDesc",list);
				modelMap.put("success",true);
			}catch (Exception e) {
				modelMap.put("success",false);
				modelMap.put("errMsg",e.toString());
			}
		}else {
			modelMap.put("success",false);
			modelMap.put("errMsg","empty reachdescId");
		}
		return modelMap;
	}
	@RequestMapping(value = "/addreachdesc",method=RequestMethod.POST)
	@ResponseBody
	private Map<String,Object> addReachDesc(@RequestBody ReachDesc reachDesc,HttpServletRequest request){
		Map<String,Object> modelMap=new HashMap<String,Object>();
		Excel currentExcel=(Excel)request.getSession().getAttribute("currentExcel");
			reachDesc.setExcelId(currentExcel.getExcelId());
		if(reachDesc!=null) {
			try {
				ReachDescExecution pe=reachDescService.InsertReachDesc(reachDesc);
				if(pe.getState()==ReachDescEnum.SUCCESS.getState()) {
					modelMap.put("success",true);
				}else {
					modelMap.put("success",false);
					modelMap.put("errMsg",pe.getStateInfo());
				}
			}catch (ReachDescOperationException e) {
				modelMap.put("success",false);
				modelMap.put("errMsg",e.toString());
				return modelMap;
			}
		}else {
			modelMap.put("success",false);
			modelMap.put("errMsg","请至少写入一种信息");
		}
		return modelMap;
	}
	@RequestMapping(value = "/modifyreachdesc",method=RequestMethod.POST)
	@ResponseBody
	private Map<String,Object> modifyReachDesc(HttpServletRequest request){
		Map<String,Object> modelMap=new HashMap<String,Object>();
		
		ObjectMapper mapper =new ObjectMapper();
		ReachDesc reachDesc=null;
		String reachDescStr= HttpServletRequestUtil.getString(request, "reachDescStr");
		try {
			reachDesc=mapper.readValue(reachDescStr,ReachDesc.class);
		}catch(Exception e) {
			modelMap.put("success", false);
			modelMap.put("errMsg", e.getMessage());
			return modelMap;
		}
		if(reachDesc!=null&&reachDesc.getWordId()>0) {
			ReachDescExecution se=reachDescService.modifyReachDesc(reachDesc);
			try {
			if(se.getState()==ReachDescEnum.SUCCESS.getState()) {
				modelMap.put("success", true);
			}else {
				modelMap.put("success", false);
				modelMap.put("errMsg", se.getStateInfo());
			}
			}catch (ReachDescOperationException e) {
				// TODO: handle exception
				modelMap.put("success",false);
				modelMap.put("errMsg",e.getMessage());
			}
			return modelMap;
		}else {
			modelMap.put("success",false);
			modelMap.put("errMsg", "课程的持续改进信息");
			return modelMap;
		}
	}
}
